package com.ncmmall.mall.controller.securitys;

import com.ncmmall.domain.security.logs.OperationLog;
import com.ncmmall.mall.params.PageParams;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;

/**
 * @author linxs
 * @Description: 操作日志
 * @Date: 2019-10-28 11:31
 */
@Controller
@RequestMapping(value = "/securitys/operation")
public class OperationLogController {

    @Autowired
    private QueryChannelService queryChannelService;

    /**
     * 操作日志
     *
     * @param model
     * @param pageParams
     * @param operationLog
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    private String list(Model model, PageParams pageParams, OperationLog operationLog) {
        QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageParams.getPageNo());
        Map<String, Object> params = Maps.newHashMap();
        String hql = builderSQL("select u from OperationLog u where  1=1 ", params, operationLog);
        QueryPage<OperationLog> page = queryChannelService.findPage(hql, pageRequest.setParams(params), OperationLog.class);
        model.addAttribute("page", page).addAttribute("params", operationLog);

        return "securitys/log/list";
    }


    private String builderSQL(String sql, Map<String, Object> params, OperationLog operationLog) {

        if (operationLog.getType() > 0) {
            sql += " and u.type = :type ";
            params.put("type", operationLog.getType());
        } else {
            sql += " and u.type = :type ";
            params.put("type", 1);
            operationLog.setType(1);
        }

        sql += " order by u.createTime desc ";

        return sql;
    }

}
